﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Administrator
{
    public partial class Sanpham : System.Web.UI.Page
    {

        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private DataTable dtProductCategorys = new DataTable();
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 DanhMucID = -1;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 30;
        private Int32 totalRecord;
        private String Search = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(EHTWebsiteProject.Administrator.Sanpham)); 
                dtProductCategorys = DanhMucSanPhamProvider.GetDanhMucSanPhams(-1, "", "").Tables[0];

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();                    
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                Session["NgonNgu"] = languageCode;

                try
                {
                    DanhMucID = Int32.Parse(Request.QueryString["DanhMucID"].ToString());                    
                }
                catch
                {
                    DanhMucID = -1;
                }

                Session["DanhMucID"] = DanhMucID.ToString();

                try
                {
                    Search = Request.QueryString["Search"].ToString().Trim();
                   
                }
                catch
                {
                    Search = "";
                }

                Session["Search"] = Search.ToString();

                try
                {
                    pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
                    Session["PageIndex"] = pageIndex.ToString();
                }
                catch
                {
                    pageIndex = 1;
                }               

                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);                             
                try
                {
                    loadProducts(Int32.Parse(Session["DanhMucID"].ToString()));
                }
                catch
                {
                    loadProducts(DanhMucID);
                }

                CreateDDLCategory(0, 0);
                if (IsVN == true)
                    this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                else
                    this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));
                ddlDanhmucSanPham.SelectedValue = DanhMucID.ToString();
            }
        }
        public SanPham GetSanPham()
        {
            SanPham theSanPham = new SanPham();
            theSanPham.DanhMucID = DanhMucID;
            if (IsVN == true)
            {
                btnselect.Text = "Chọn";
                theSanPham.TenSanPhamVN = Search;
                theSanPham.TenSanPhamOther = "";
            }
            else
            {
                btnselect.Text = "Select";
                theSanPham.TenSanPhamOther = Search;
                theSanPham.TenSanPhamVN = "";
            }
            return theSanPham;
        }

        public void loadProducts(Int32 DanhMucID)
        {
            DataSet ds = SanPhamProvider.GetSanPhamsByFilter(GetSanPham(), pageIndex, pageSize, out totalRecord);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (Session["NgonNgu"].ToString() == "vi-VN")
                {
                    rpProducts.DataSource = ds;
                    rpProducts.DataBind();
                    pnSanPhamOther.Visible = false;
                }
                else
                {
                    rpProductsOther.DataSource = ds;
                    rpProductsOther.DataBind();
                    pnSanPham.Visible = false;
                }
            }
            else
            {
                pnSanPham.Visible = false;
                pnSanPhamOther.Visible = false;
                if (Session["NgonNgu"].ToString() == "vi-VN")
                    ltrData.Text = HangSo.timkiem;
                else
                    ltrData.Text = HangSo.timkiemOther;
            }
            this.PaggingControl1.PageIndex = pageIndex;
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PageURL = "Sanpham.aspx?DanhMucID=" + DanhMucID.ToString();
        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucID"].ToString());
                ListItem li;
                if (IsVN == true)
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucVN"].ToString(), drCategorysSub[i]["DanhMucID"].ToString());
                else
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucOther"].ToString(), drCategorysSub[i]["DanhMucID"].ToString());
                this.ddlDanhmucSanPham.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }

        protected void ddlDanhmucSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Sanpham.aspx?DanhMucID=" + ddlDanhmucSanPham.SelectedValue);
        }

        protected void ImagSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Sanpham.aspx?Search=" + txtsearch.Text);
        }

        protected void btnselect_Click(object sender, EventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session["NgonNgu"].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpProducts.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpProducts.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }

            if (arr.Count > 1 || arr.Count == 0)
            {
                if (IsVN == true)
                    ltrErorr.Text = HangSo.Eorr_Edit;
                else
                    ltrErorr.Text = HangSo.Eorr_EditOther;               
            }
            else
            {
                Session["value"] = Int32.Parse(arr[0].ToString());            
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myCloseScript", "GetRadWindow().close()", true);                                
            }
        }       
       
    }
}